package com.xuchang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuchang.domain.SysUser;
import com.xuchang.domain.SysUserRole;
import com.xuchang.model.R;
import com.xuchang.service.SysUserRoleService;
import com.xuchang.service.SysUserService;
import com.xuchang.mapper.SysUserMapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author xuchang
* @description 针对表【sys_user(平台用户)】的数据库操作Service实现
* @createDate 2025-06-29 22:19:07
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public Page<SysUser> searchUsers(Page page, String phone, String name) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<SysUser>().like(!StringUtils.isEmpty(phone), SysUser::getMobile, phone).like(!StringUtils.isEmpty(name), SysUser::getFullname, name);
        Page<SysUser> pageData = page(page, wrapper);
        List<SysUser> records = pageData.getRecords();
        for (SysUser record : records) {
            LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, record.getId());
            List<SysUserRole> list = sysUserRoleService.list(queryWrapper);
            String collect = list.stream().map(sysUserRole -> sysUserRole.getRoleId().toString()).collect(Collectors.joining(","));
            record.setRole_strings(collect);
        }
        return pageData;
    }

    @Override
    public boolean createUser(SysUser sysUser) {
        String roleStrings = sysUser.getRole_strings();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);
        boolean saved = super.save(sysUser);
        if(saved){
            if(!StringUtils.isEmpty(roleStrings)){
                String[] roleIds = roleStrings.split(",");
                ArrayList<SysUserRole> sysUserRoles = new ArrayList<>(roleIds.length);
                for (String role : roleIds) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(Long.valueOf(role));
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRoles.add(sysUserRole);
                }
                sysUserRoleService.saveBatch(sysUserRoles);
            }
        }
        return saved;
    }

    @Override
    public boolean editUser(SysUser sysUser) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);
        boolean saved = super.saveOrUpdate(sysUser);
        if(saved){
            Long id = sysUser.getId();
            sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId,id));
            String roleStrings = sysUser.getRole_strings();
            if(!StringUtils.isEmpty(roleStrings)){
                String[] roleIds = roleStrings.split(",");
                ArrayList<SysUserRole> sysUserRoles = new ArrayList<>(roleIds.length);
                for (String role : roleIds) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(Long.valueOf(role));
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRoles.add(sysUserRole);
                }
                sysUserRoleService.saveBatch(sysUserRoles);
            }
        }
        return saved;
    }

    @Override
    public boolean deleteByIds(List<Long> list) {
        boolean b = super.removeByIds(list);
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, list);
        sysUserRoleService.remove(queryWrapper);
        return b;
    }
}




